package com.cqupt.controller;

import com.alibaba.fastjson2.JSON;
import com.cqupt.model.vo.ImageCheck;
import com.cqupt.model.vo.Result;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.model.vo.ScoreVO;
import com.cqupt.service.SparkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Tag(name = "讯飞星火AI模块")
@RequestMapping("/api/spark")
public class SparkController {

    @Resource
    private SparkService sparkService;

    @GetMapping("/sendMsg")
    @Operation(summary = "星火3.5接口v1.0")
    public Result sendMessageToSpark(String message) {
        String response = sparkService.sendMessageToSpark(message);
        return Result.build(response, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/chat")
    @Operation(summary = "星火3.5接口v2.0")
    public Result chat(@RequestParam("id") int id, @RequestParam("message") String message) {
        String response = sparkService.chat(id, message);
        return Result.build(response, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/recognize")
    @Operation(summary = "语音识别")
    public Result recognize(@RequestParam("file") MultipartFile file) {
        String message = sparkService.recognize(file);
        System.out.println(message);
        return Result.build(message, ResultCodeEnum.SUCCESS);
//        String response = sparkService.chat(id, message);
//        return Result.build(response, ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/synthesize")
    @Operation(summary = "语音合成")
    public Result synthesize(String text) {
        byte[] res = sparkService.tts(text);
        return Result.build(res, ResultCodeEnum.SUCCESS);
    }


    @PostMapping("/checkImageCompliance")
    @Operation(summary = "图片检测")
    public Result checkImageCompliance(@RequestParam("file") MultipartFile file) {
        String json = sparkService.checkImageCompliance(file);
        return Result.build(JSON.parseObject(json, ImageCheck.class), ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/checkText")
    @Operation(summary = "文本检测")
    public Result checkText(@RequestParam("text") String text) {
        String json = sparkService.checkText(text);
        return Result.build(JSON.parseObject(json, ImageCheck.class), ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "语音测评")
    @PostMapping("/evaluate")
    public Result evaluate(@RequestParam("file") MultipartFile file, @RequestParam("text") String text) {
        ScoreVO res = sparkService.evaluate(file, text);
        return Result.build(res, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "机器翻译")
    @PostMapping("/translate")
    public Result translate(@RequestParam("text") String text) {
        String res = sparkService.translate(text);
        return Result.build(res, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "音频合规检测")
    @PostMapping("/audioCheck")
    public Result audioCheck(@RequestParam("file") MultipartFile file) {
        String res = sparkService.audioCheck(file);
        return Result.build(res, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/video_callback")
    public void handleAudioCallback(@RequestBody String callbackData) {
        // 处理回调数据
        System.out.println("Received callback data: " + callbackData);
        // 这里你可以根据需求处理回调数据，比如保存到数据库或触发其他业务逻辑
    }


}
